'''
Test that blog views are working as intended.
'''

from django.test import TestCase
from django.test.client import Client
from blogs.models import Blog, Post, Tag, Comment
from blogs.forms import CommentForm

class BlogTests(TestCase):
    '''
    These tests will test the functionality of the main blog views.
    i.e. what users will see when they visit the blog.
    '''
    
    fixtures = ['blogs.json']
        
    def test_main_page(self):
        response = self.client.get('/blogs/foo-bar/')
        self.failUnlessEqual(response.status_code, 200)
        
    def test_login_admin(self):
        self.client.login(username='jhsu', password='1234')
        response = self.client.get('/blogs/foo-bar/')
        self.assertContains(response, 'Edit this post')
        
    def test_paging(self):
        response = self.client.get('/blogs/foo-bar/page/2/')
        self.assertContains(response, 'We can be stupider than me')
        
    def test_page_navigation_links(self):
        response = self.client.get('/blogs/foo-bar/')
        self.assertContains(response, '<a href="/blogs/foo-bar/page/2/">')
        
        response = self.client.get('/blogs/foo-bar/page/2/')
        self.assertContains(response, '<a href="/blogs/foo-bar/page/1/">')
        self.assertContains(response, '<a href="/blogs/foo-bar/page/3/">')
        
        response = self.client.get('/blogs/foo-bar/page/3/')
        self.assertContains(response, '<a href="/blogs/foo-bar/page/2/">')
    
    def test_comment_valid(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        valid_data = {'name': 'Dude', 'email': 'dude@globeandmail.ca', 'message': 'This is a test comment!'}
        
        response = c.post('/blogs/foo-bar/2008/03/18/hello-world/', valid_data)
        self.assertContains(response, '<a href="mailto:dude@globeandmail.ca">Dude</a>')
        self.assertContains(response, 'This is a test comment!')
    
    def test_comment_blank_fields(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        
        response = c.post('/blogs/foo-bar/2008/03/18/hello-world/', {})
        self.assertContains(response, 'This field is required.', count=3)
    
    def test_comment_no_name(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        no_name_data = {'email': 'jhsu@globeandmail.ca', 'message': 'This is a test comment!'}
        
        response = c.post('/blogs/foo-bar/2008/03/18/hello-world/', no_name_data)
        self.assertFormError(response, 'comment_form', 'name', 'This field is required.')
        
    def test_comment_no_email(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        no_email_data = {'name': 'Jack', 'message': 'This is a test comment!'}
        
        response = c.post('/blogs/foo-bar/2008/03/18/hello-world/', no_email_data)
        self.assertFormError(response, 'comment_form', 'email', 'This field is required.')
        
    def test_comment_no_msg(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        no_msg_data = {'name': 'Jack', 'email': 'jhsu@globeandmail.ca'}
        
        response = c.post('/blogs/foo-bar/2008/03/18/hello-world/', no_msg_data)
        self.assertFormError(response, 'comment_form', 'message', 'This field is required.')
        
    def test_comment_invalid_name(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        invalid_email_data = {'name': 'Jack', 'email': 'not an email address', 'message': 'This is a test comment!'}
        
        response = c.post('/blogs/foo-bar/2008/03/18/hello-world/', invalid_email_data)
        self.assertFormError(response, 'comment_form', 'email', 'Enter a valid e-mail address.')
         
    def test_comments_closed(self):
         c = Client(REMOTE_ADDR='127.0.0.1')
         response = c.get('/blogs/foo-bar/2008/03/18/lorem-ipsum/')
         self.assertContains(response, 'Comments are closed for this post.')
         
    def test_banned_ip(self):
        c = Client(REMOTE_ADDR='255.255.255.255') # 255.255.255.255 has been banned
        response = c.get('/blogs/foo-bar/2008/03/18/hello-world/')
        self.assertContains(response, 'Your IP (255.255.255.255) has been banned from commenting on this blog.')
        
    def test_post_tags(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        response = c.get('/blogs/foo-bar/2008/03/18/django-rules/')
        self.assertContains(response, '<a href="/blogs/foo-bar/tags/django/">django</a>')
        self.assertContains(response, '<a href="/blogs/foo-bar/tags/python/">python</a>')
        
    def test_post_no_tags(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        response = c.get('/blogs/foo-bar/2008/03/20/codesod-alphabet-hard-way/')
        self.assertContains(response, 'No tags')
        
    def test_tag_view(self):
        c = Client(REMOTE_ADDR='127.0.0.1')
        response = c.get('/blogs/foo-bar/tags/computer/')
        self.assertContains(response, '<p>Posts tagged with <em>computer</em>.</p>')
        
    def test_tag_cloud(self):
        response = self.client.get('/blogs/foo-bar/')
        self.assertContains(response, '<a href="/blogs/foo-bar/tags/computer/" style="font-size: 135%">computer</a>')
        self.assertContains(response, '<a href="/blogs/foo-bar/tags/ipsum/" style="font-size: 105%">ipsum</a>')
        self.assertContains(response, '<a href="/blogs/foo-bar/tags/lorem/" style="font-size: 130%">lorem</a>')
        self.assertContains(response, '<a href="/blogs/foo-bar/tags/django/" style="font-size: 100%">django</a> ')
        